home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / text / hyper / ADtoHT2_0.lha / MyLib.lha / stdlib / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  965 b   |  53 lines

  1. #include <stdlib.h>
  2. #include <errno.h>
  3.  
  4. #include <proto/exec.h>
  5.  
  6. #if defined(__SASC_510)
  7. void *LibCreatePool(ULONG, ULONG, ULONG);
  8. void *LibAllocPooled(void *, ULONG);
  9. void LibDeletePool(void *);
  10. #else
  11. #include <clib/alib_protos.h>
  12. #endif
  13.  
  14. #include "Internal.h"
  15.  
  16. /*************************************************************************/
  17.  
  18. void *__MemoryPool;
  19.  
  20. /*************************************************************************/
  21.  
  22. void *malloc(size_t Size)
  23.  
  24. {
  25.   unsigned long *Memory;
  26.  
  27.   /* create memory pool if necessary */
  28.   if (!__MemoryPool)
  29.     {
  30.       if ((__MemoryPool=LibCreatePool(0,64*1024,32*1024)))
  31.     {
  32.       MemoryCleanup=LibDeletePool;
  33.     }
  34.       else
  35.     {
  36.       errno=ENOMEM;
  37.       return NULL;
  38.     }
  39.     }
  40.  
  41.   /* do the allocation */
  42.   Size=(Size+sizeof(*Memory)+4-1)&~3;        /* make sure size is longword multiple */
  43.   if ((Memory=LibAllocPooled(__MemoryPool,Size)))
  44.     {
  45.       *(Memory++)=Size;
  46.     }
  47.   else
  48.     {
  49.       errno=ENOMEM;
  50.     }
  51.   return Memory;
  52. }
  53.